Ext.namespace('Divo');
Ext.namespace('Divo.app');
Divo.app.ValidationList = function() {
  /* ----------------------- private属性 ----------------------- */
  var gridEl = 'Validation-grid';
  var grid, selModel, toolbar, win;
  var data;
  var detailGrid, detailModel;
  var stockInId;
  /* ----------------------- private方法 ----------------------- */
  // 创建列表
  function createGrid() {
    var queryFlds = new Array();
    var searchFlds = new Ext.util.MixedCollection();
    queryFlds.push('name');
    var fldName = new Ext.form.TextField({
          name : 'name',
          id : gridEl + '-name',
          fieldLabel : '名称',
          allowBlank : true,
          width : 100

        });
    searchFlds.add('name', fldName);

    grid = new Divo.Base.GridView({
          remoteSort : true,
          gridId : gridEl + "si",
          queryFlds : queryFlds,
          searchFlds : searchFlds,
          entityClassName : 'com.our.sys.project1.entity.StockIn',
          queryUrl : 'stock_in/query_stock_in',
          jsonId : 'id',
          recordPk : ["id"]
        });

    detailGrid = new Divo.Base.GridView({
          remoteSort : true,
          gridId : gridEl + "detail",
          entityClassName : 'com.our.sys.project1.entity.StockDetail',
          queryUrl : 'stock_in/query_stock_detail',
          jsonId : 'id',
          recordPk : ["id"]
        });

    selModel = grid.getSelectionModel();
    selModel.on('rowselect', onRowSelect);

    detailGrid.getStore().on('beforeload', function(store, options) {
          store.baseParams.stockInId = stockInId;
        }, this, true);
  }

  // 刷新列表
  function reloadRecords() {
    grid.getStore().reload();
  }

  function onRowSelect(selModel, rowIndex, record) {
    var data = record.data;
    stockInId = data.id;
    detailGrid.getStore().load();
  }

  // 创建layout
  function createLayout() {
    toolbar = new Ext.Toolbar({
          items : [{
                xtype : "button",
                cls : "x-btn-icon",
                icon : "resources/img/g_rec_src.png",
                tooltip : "过滤数据",
                id : "tlb_FILTER",
                handler : toggleFilterBar
              }, "-", {
                icon : "resources/img/g_rec_new.png",
                text : "验收",
                id : "tlb_DISCOUNT",
                handler : validation
              }, "-", {
                icon : "resources/img/g_rec_refresh.gif",
                text : "刷新",
                id : "tlb_REFRESH",
                handler : reloadRecords
              }]
        });
    var viewport = new Ext.Viewport({
          layout : 'border',
          items : [{
                layout : 'border',
                region : 'center',
                border : false,
                items : [{
                      layout : 'fit',
                      region : 'north',
                      border : false,
                      items : [toolbar],
                      height : 25
                    }, {
                      layout : 'fit',
                      region : 'center',
                      border : false,
                      items : [grid]
                    }, {
                      layout : 'fit',
                      region : 'south',
                      border : false,
                      items : [detailGrid],
                      height : 200
                    }]
              }]
        });
    viewport.doLayout();
  }

  function validation() {
    // 判断是否已经填写价格和数量
    var sels = grid.getSelectionModel().getSelections();
    if (sels.length <= 0) {
      Ext.Msg.alert('错误', "请选择一条记录！");
      return;
    }
    Ext.Ajax.request({
          params : {
            stockInId : stockInId
          },
          url : "stock_in/validation",
          callback : function(o, s, r) {
            var respText = Ext.decode(r.responseText);
            if (respText.success) {
              grid.getStore().load();
              detailGrid.getStore().load();
            } else {
              if (respText.message.length > 0) {
                Ext.Msg.alert('错误', respText.message);
              }
            }
          }
        });
  }

  // 切换过滤查询工具条状态
  function toggleFilterBar() {
    grid.toggleFilterBar();
  }

  /* ----------------------- public方法 ----------------------- */
  return {
    /**
     * 初始化
     */
    init : function() {
      createGrid();
      createLayout();
      grid.getStore().load();
    }
  }
}();

Ext.onReady(Divo.app.ValidationList.init, Divo.app.ValidationList, true);